﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Remindme.Exceptions;
using Remindme.Model;
using Remindme.Repository.Interfaces;
using Remindme.Services.Interfaces;
using Remindme.ViewModel.Interfaces;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Remindme.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase, IMainViewModel
    {
        private readonly IDataService _dataService;
        private readonly IItemRepository _itemRepository;
        private readonly INavigationService _navigationService;
        private readonly IArticleRepository _articleRepository;

        #region Propiedades de enlace
        /// <summary>
        /// The <see cref="AllItems" /> property's name.
        /// </summary>
        public const string AllItemsPropertyName = "AllItems";

        private ObservableCollection<ItemModel> _allItems = new ObservableCollection<ItemModel>();

        /// <summary>
        /// Sets and gets the AllItems property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<ItemModel> AllItems
        {
            get
            {
                return _allItems;
            }

            set
            {
                if (_allItems == value)
                {
                    return;
                }

                RaisePropertyChanging(AllItemsPropertyName);
                _allItems = value;
                RaisePropertyChanged(AllItemsPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SelectedItem" /> property's name.
        /// </summary>
        public const string SelectedItemPropertyName = "SelectedItem";

        private ItemModel _selectedItem = null;

        /// <summary>
        /// Sets and gets the SelectedItem property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ItemModel SelectedItem
        {
            get
            {
                return _selectedItem;
            }

            set
            {
                if (_selectedItem == value)
                {
                    return;
                }

                RaisePropertyChanging(SelectedItemPropertyName);
                _selectedItem = value;
                RaisePropertyChanged(SelectedItemPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IsDataLoaded" /> property's name.
        /// </summary>
        public const string IsDataLoadedPropertyName = "IsDataLoaded";

        private bool _isDataLoaded = false;

        /// <summary>
        /// Sets and gets the IsDataLoaded property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsDataLoaded
        {
            get
            {
                return _isDataLoaded;
            }

            set
            {
                if (_isDataLoaded == value)
                {
                    return;
                }

                RaisePropertyChanging(IsDataLoadedPropertyName);
                _isDataLoaded = value;
                RaisePropertyChanged(IsDataLoadedPropertyName);
            }
        }
        #endregion Propiedades de enlace

        public RelayCommand ViewItemDetailCommand { get; private set; }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(
            IDataService dataService, 
            IItemRepository itemRepository,
            INavigationService navigationService,
            IArticleRepository articleRepository)
        {
            _dataService = dataService;
            //_dataService.GetData(
            //    (item, error) =>
            //    {
            //        if (error != null)
            //        {
            //            // Report error here
            //            return;
            //        }

            //        WelcomeTitle = item.Title;
            //    });
            _itemRepository = itemRepository;
            _navigationService = navigationService;
            _articleRepository = articleRepository;

            ViewItemDetailCommand = new RelayCommand(this.ViewItemDetail);
        }

        async public Task LoadDataAsync()
        {
            try
            {
                var items = await _itemRepository.FindAllAsync();
                foreach (var i in items)
                {
                    AllItems.Add(i);
                }
                IsDataLoaded = true;
            }
            catch (CustomException ce)
            {
                System.Diagnostics.Debug.WriteLine(ce.Message);
            }
        }

        async void ViewItemDetail()
        {
            var article = await _articleRepository.ResolveArticleAsync(SelectedItem.resolved_url);
            _navigationService.NavigateTo(typeof(DetailPage), article);
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}